package com.sxj.PDMS.controller.admin;

import com.sxj.PDMS.common.BaseResponse;
import com.sxj.PDMS.common.ErrorCode;
import com.sxj.PDMS.common.Result;
import com.sxj.PDMS.exception.BusinessException;
import com.sxj.PDMS.exception.ThrowUtils;
import com.sxj.PDMS.model.dto.admin.PartyMemberAwardsRequest;
import com.sxj.PDMS.model.dto.admin.PartyMemberAwardsUpdateRequest;
import com.sxj.PDMS.model.dto.admin.PartyMemberPunishmentsRequest;
import com.sxj.PDMS.model.dto.admin.PartyMemberPunishmentsUpdateRequest;
import com.sxj.PDMS.model.entity.PartyMemberAwards;
import com.sxj.PDMS.model.entity.PartyMemberPunishments;
import com.sxj.PDMS.model.vo.PagingQueryResponse;
import com.sxj.PDMS.service.PartyMemberAwardsService;
import com.sxj.PDMS.service.PartyMemberPunishmentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.time.LocalDateTime;

import static com.sxj.PDMS.utils.VerifyUtil.formatDate;

@CrossOrigin
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminAwardsAndPunishmentController {
    @Autowired
    private PartyMemberAwardsService partyMemberAwardsService;
    @Autowired
    private PartyMemberPunishmentsService partyMemberPunishmentsService;
    @PostMapping("/upload_awards")
    public BaseResponse awardsUpload(PartyMemberAwardsRequest partyMemberAwardsRequest) throws ParseException {
        log.info("奖励上传：{}", partyMemberAwardsRequest);
        if(partyMemberAwardsRequest.getPartyMemberNumber()==null &&partyMemberAwardsRequest.getBonusContent()==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean saved = partyMemberAwardsService.awardsUpload(partyMemberAwardsRequest);
        if (!saved) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return Result.success();
    }

    @PutMapping("/modify_awards")
    public BaseResponse<Boolean> updateAwards(@RequestBody PartyMemberAwardsUpdateRequest partyMemberAwardsUpdateRequest) throws ParseException {
        log.info("修改奖励：{}", partyMemberAwardsUpdateRequest);

        if(partyMemberAwardsUpdateRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        PartyMemberAwards updatePartyMemberAwards = new PartyMemberAwards();

        BeanUtils.copyProperties(partyMemberAwardsUpdateRequest, updatePartyMemberAwards);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        updatePartyMemberAwards.setAwardDate(formatDate(currentTime));
        boolean result = partyMemberAwardsService.updateById(updatePartyMemberAwards);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return Result.success(true);
    }

    @DeleteMapping("/delete_awards")
    public BaseResponse<Boolean> deleteAwards(Long serialNumber) {
        if (serialNumber == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean removed = partyMemberAwardsService.removeById(serialNumber);
        if (!removed) {
            //todo://删除的序号不存在时，显示错误信息
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return Result.success(true);
    }

    @GetMapping("/selectAll_awards")
    public BaseResponse<PagingQueryResponse<PartyMemberAwards>> selectAwards(@RequestParam(defaultValue = "1") Integer page,
                                                                    @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询 ：page:{}, pageSize:{}", page, pageSize);
        PagingQueryResponse<PartyMemberAwards> pagingQueryResponse = partyMemberAwardsService.selectAll(page, pageSize);
        return Result.success(pagingQueryResponse);

    }

    @GetMapping("/select_awards")
    public BaseResponse<PagingQueryResponse<PartyMemberAwards>> selectAwardsWithCondition(@RequestParam(defaultValue = "1") Integer page,
                                                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                    PartyMemberAwards partyMemberAwards) {

        PagingQueryResponse<PartyMemberAwards> pagingQueryResponse = partyMemberAwardsService.awardsSelect(page, pageSize, partyMemberAwards);

        return Result.success(pagingQueryResponse);
    }

    @PostMapping("/upload_punishments")
    public BaseResponse punishmentsUpload( PartyMemberPunishmentsRequest partyMemberPunishmentsRequest) throws ParseException {
        log.info("惩罚上传：{}", partyMemberPunishmentsRequest);
        if(partyMemberPunishmentsRequest.getPartyMemberNumber()==null &&partyMemberPunishmentsRequest.getPunishmentContent()==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean saved = partyMemberPunishmentsService.punishmentsUpload(partyMemberPunishmentsRequest);
        if (!saved) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return Result.success();
    }

    @PutMapping("/modify_punishments")
    public BaseResponse<Boolean> updatePunishments(@RequestBody PartyMemberPunishmentsUpdateRequest partyMemberPunishmentsUpdateRequest) throws ParseException {
        log.info("修改惩罚：{}", partyMemberPunishmentsUpdateRequest);
        if(partyMemberPunishmentsUpdateRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PartyMemberPunishments partyMemberPunishments =new PartyMemberPunishments();
        BeanUtils.copyProperties(partyMemberPunishmentsUpdateRequest, partyMemberPunishments);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        partyMemberPunishments.setPunishmentDate(formatDate(currentTime));
        boolean result = partyMemberPunishmentsService.updateById(partyMemberPunishments);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return Result.success(true);
    }

    @DeleteMapping("/delete_punishments")
    public BaseResponse<Boolean> deletePunishments(Long serialNumber) {
        if (serialNumber == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean removed = partyMemberPunishmentsService.removeById(serialNumber);
        if (!removed) {
            //todo://删除的序号不存在时，显示错误信息
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return Result.success(true);
    }

    @GetMapping("/selectAll_punishments")
    public BaseResponse<PagingQueryResponse<PartyMemberPunishments>> selectPunishments(@RequestParam(defaultValue = "1") Integer page,
                                                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询 ：page:{}, pageSize:{}", page, pageSize);
        PagingQueryResponse<PartyMemberPunishments> pagingQueryResponse = partyMemberPunishmentsService.selectAll(page, pageSize);
        return Result.success(pagingQueryResponse);

    }

    @GetMapping("/select_punishments")
    public BaseResponse<PagingQueryResponse<PartyMemberPunishments>> selectPunishmentsWithCondition(@RequestParam(defaultValue = "1") Integer page,
                                                                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                                    PartyMemberPunishments partyMemberPunishments) {

        PagingQueryResponse<PartyMemberPunishments> pagingQueryResponse = partyMemberPunishmentsService.punishmentsSelect(page, pageSize, partyMemberPunishments);

        return Result.success(pagingQueryResponse);
    }

}
